<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/CustomCommand.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/DaoFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/utilities/PHPMailer/class.phpmailer.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/utilities/PHPMailer/class.smtp.php';

	
	error_reporting (E_ALL ^ E_NOTICE);
	
	$debug = $_SESSION['debug'];
	
	/*
	* Class to send email
	*/
	class CommandSendEmail extends CustomCommand {
		/*
		* Constructor of the class
		*/
		public function __construct($email) {
			$this -> email = $email;
			
		}
		
		/*
		* Method to execute to send email
		*/
		public function execute() {
			$daoFactory = DaoFactory :: getMySqlDaoFactory();
			$mail = $daoFactory-> getMySqlUserAccessDao();			
			$GLOBALS['debug'] -> log($this -> email,"Entro al metodo ejecutar del comando send email en comando");
			$sentence = $mail -> sendEmail($this -> email);
	
		 if ($sentence)
		 {	
		 $GLOBALS['debug'] -> log("entro if comando si sentence tiene data");
			$user = new User("","","","","","",$this -> email,"");
			
			$row = mysql_fetch_array($sentence);
			
			
			$user-> setPkUsername($row["pk_username"]);	 
			$user-> setPassword($row["password"]);
			$user-> setEmailAddress($row["email_address"]);
			
			$GLOBALS['debug'] -> log($user->getPkUsername(),"nombre");
			$GLOBALS['debug'] -> log($user->getPassword(),"clave");
			$GLOBALS['debug'] -> log($user->getEmailAddress(),"email");
			
				if ($user)		
				{
				
					$GLOBALS['debug'] -> log("si el objeto usuario tiene data en el comando para llamar a constructMail");		
					$GLOBALS['debug'] -> log(" entro a la configuracion del mail");	
					$GLOBALS['debug'] -> log($user->getPkUsername()," entro a la configuracion del mail");
					$GLOBALS['debug'] -> log($user->getPassword()," entro a la configuracion del mail");	
					$GLOBALS['debug'] -> log($user->getEmailAddress()," entro a la configuracion del mail");
							
							
					/*configuration mail*/
					$mail = new PHPMailer();
					$mail->IsSMTP();
					$mail->SMTPAuth = true;
					$mail->SMTPSecure = "ssl";
					$mail->Host = "smtp.gmail.com";
					$mail->Port = 465;
					$mail->Username = "fenix02ve@gmail.com";
					$mail->Password = "alfredoysabrina123";
					
				$GLOBALS['debug'] -> log(" termino la configuracion de mail");	
								
					/* contruct mail*/
					$mail->From = "fenix02ve@domain.com";
					$mail->FromName = "Ductobarra";
					$mail->Subject = "Recuperacion de Nombre de Usuario y Clave";
					$mail->AltBody = "";
					$mail->MsgHTML("Buenas, en esta oportunidad nos dirigimos a usted para informarle que su nombre de usuario es ".$user->getPkUsername()."   y su clave es ".$user->getPassword());
					$mail->AddAttachment("");
					$mail->AddAttachment("");
					$mail->AddAddress($user->getEmailAddress(), "Destinatario");
					$mail->IsHTML(true); 
					
					if(!$mail->Send()) {
					 
					  $GLOBALS['debug'] -> log("error en el envio");	 
					} else {
					  $GLOBALS['debug'] -> log(" envio correcto");
					  
					}
			  	$GLOBALS['debug'] -> log("salio del metodo constructMail ");
				  
				}
				else
				{
				 $this -> result=NULL;			
				}
			
			$GLOBALS['debug'] -> log($email,"Salio del comando send email ");		
			$this -> result = $user;
			
		 }
		 else
		 {
			 $this -> result=NULL;		 
	     }
		 
			
	}
		
		/*
			contruct mail message.			
		*/
		public function constructMail($name,$password,$mail)
		{		
		$GLOBALS['debug'] -> log(" entro a la configuracion del mail");	
				$GLOBALS['debug'] -> log($name," entro a la configuracion del mail");
				$GLOBALS['debug'] -> log($password," entro a la configuracion del mail");	
				$GLOBALS['debug'] -> log($mail," entro a la configuracion del mail");
				
					
			/*configuration mail*/
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = "ssl";
			$mail->Host = "smtp.gmail.com";
			$mail->Port = 465;
			$mail->Username = "fenix02ve@gmail.com";
			$mail->Password = "alfredoysabrina12";
			
	    $GLOBALS['debug'] -> log(" termino la configuracion de mail");	
						
			/* contruct mail*/
			$mail->From = "fenix02ve@domain.com";
			$mail->FromName = "User Name";
			$mail->Subject = "Recuperacion de Nombre de Usuario y Clave";
			$mail->AltBody = "";
			$mail->MsgHTML("Buenas, en esta oportunidad nos dirigimos a usted para informarle que su nombre de usuario es ".$name."   y su clave es ".$password);
			$mail->AddAttachment("");
			$mail->AddAttachment("");
			$mail->AddAddress($mail, "Destinatario");
			$mail->IsHTML(true);
			
			$GLOBALS['debug'] -> log("envio correcto del correo");	 
			
			if(!$mail->Send()) {
			  echo "Error: " . $mail->ErrorInfo;
			} else {
			  echo "Mensaje enviado correctamente";
			}
						
	
		}
		
		
		
	}
?>